package com.wy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wy.common.Result;
import com.wy.entity.GoodsType;
import com.wy.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:王渝
 * @Date: 2022/12/9  9:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/type")
public class GoodsTypeController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询所有类型
     * @return
     */
    //@Cacheable(value = "typeCache1" )
    @RequestMapping("/getAllType")
    public Result<List<String>> queryAllType(){
        List<String> types = (List<String>) redisTemplate.opsForHash().get("type", "1");
        if(types != null){
            return Result.success(types);
        }
        LambdaQueryWrapper<GoodsType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(GoodsType::getTypeName);
        //select type_name form goods_type
        List<GoodsType> list = goodsTypeService.list(queryWrapper);
        List<String> type = new ArrayList<>();
        for (GoodsType goodsType : list) {
            type.add(goodsType.getTypeName());
        }
        redisTemplate.opsForHash().put("type", "1",type);
        return Result.success(type);
    }

    /**
     * 查询所有类型2(返回值带上typeId的)
     * @return
     */
   // @Cacheable(value = "typeCache2" )
    @RequestMapping("/getAllType2")
    public Result<List<GoodsType>> queryAllType2(){
        List<GoodsType> types = (List<GoodsType>) redisTemplate.opsForHash().get("type", "2");
        if(types != null){
            return Result.success(types);
        }
        LambdaQueryWrapper<GoodsType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(GoodsType::getTypeName,GoodsType::getTypeId);
        //select type_name form goods_type
        List<GoodsType> list = goodsTypeService.list(queryWrapper);
        redisTemplate.opsForHash().put("type", "2",list);
        return Result.success(list);
    }


}
